<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item  label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.zhwy_chargeitem_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label-width="100px" label="收费项目名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['feeSettings:chargeitem:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['feeSettings:chargeitem:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['feeSettings:chargeitem:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['feeSettings:chargeitem:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chargeitemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" width="50px" />
      <el-table-column label="类型" align="center" prop="type" width="150px">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhwy_chargeitem_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="收费项目名" align="center" prop="name" />
      <el-table-column label="单价" align="center" prop="univalence" />
      <el-table-column label="收费方式" align="center" prop="mode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhwy_chargeitem_mode" :value="scope.row.mode"/>
        </template>
      </el-table-column>
      <el-table-column label="计算公式" align="center" prop="formula" />
      <el-table-column label="计算周期" align="center" prop="period">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhwy_period" :value="scope.row.period"/>
        </template>
      </el-table-column>
      <el-table-column label="滞纳金" align="center" prop="latefee">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zhwy_latefee" :value="scope.row.latefee"/>
        </template>
      </el-table-column>
      <el-table-column label="滞纳金比例" align="center" prop="latefeeProportion" />
      <el-table-column label="滞纳金天数" align="center" prop="latefeeDay" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['feeSettings:chargeitem:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['feeSettings:chargeitem:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收费项管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="dict in dict.type.zhwy_chargeitem_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收费项目名" prop="name">
          <el-input v-model="form.name" placeholder="请输入" />
        </el-form-item>
        <div v-if="form.type == 0">
          <el-form-item label="单价" prop="univalence">
            <el-input v-model="form.univalence" placeholder="请输入单价" />
          </el-form-item>
           <el-form-item label="收费方式" prop="mode">
            <el-select v-model="form.mode" placeholder="请选择">
              <el-option
                v-for="dict in dict.type.zhwy_chargeitem_mode"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="计算公式" prop="formula">
            <el-input v-model="form.formula"  readonly placeholder="请输入计算公式" />
          </el-form-item>
          <el-form-item label="计算周期" prop="period">
            <el-select v-model="form.period" placeholder="几个月收费一次">
              <el-option
                v-for="dict in dict.type.zhwy_period"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="滞纳金" prop="latefee">
            <el-radio-group  v-model="form.latefee">
              <el-radio
                v-for="dict in dict.type.zhwy_latefee"
                :key="dict.value"
                :label="parseInt(dict.value)"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <div v-if="form.latefee == 1">
             <el-form-item label="滞纳金比例" prop="latefeeProportion">
              <el-input v-model="form.latefeeProportion" placeholder="请输入" >
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
            <el-form-item label="滞纳金天数" prop="latefeeDay">
              <el-input v-model="form.latefeeDay" placeholder="请输入" />
            </el-form-item>
          </div>
        </div>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChargeitem, getChargeitem, delChargeitem, addChargeitem, updateChargeitem } from "@/api/feeSettings/chargeitem";

export default {
  name: "Chargeitem",
  dicts: ['zhwy_chargeitem_type', 'zhwy_period', 'zhwy_chargeitem_mode', 'zhwy_latefee'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收费项管理表格数据
      chargeitemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        name: null,
      },
      // 表单参数
      form: {
        type:'',
        formula:'',
        latefee:'',
        latefeeProportion:'',
        latefeeDay:'',
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入收费项目名", trigger: "blur" },
        ],
        type: [
          { required: true },
        ],
        // univalence: [
        //   { required: true, message: "单价不能为空", trigger: "blur" },
        // ],
        // mode: [
        //   { required: true },
        // ],
        // formula: [
        //   { required: true },
        // ],
        // period: [
        //   { required: true},
        // ],

        // latefeeProportion: [
        //   { required: true, message: "滞纳金比例不能为空", trigger: "blur" },
        // ],
        // latefeeDay: [
        //   { required: true, message: "滞纳金天数不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  watch: {
    'form.latefee'(newVal,oldVal) {
      console.log(newVal,oldVal);
      if (newVal==0) {
        this.form.latefeeProportion = 0;
        this.form.latefeeDay = 0;
      }
    },
    'form.type'(newVal) {
      if (this.form.type==0 && this.title=="添加收费项管理") {
        this.form.formula = "单价*数量";
        this.form.latefee = 0;
      }else if(this.form.type == 1 || this.form.type == 2){

      }
    }
  },

  created() {
    this.getList();
  },
  methods: {
    /** 查询收费项管理列表 */
    getList() {
      this.loading = true;
      listChargeitem(this.queryParams).then(response => {
        this.chargeitemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        name: null,
        univalence: null,
        mode: null,
        formula: null,
        period: null,
        latefee: null,
        latefeeProportion: null,
        latefeeDay: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收费项管理";
      // //单选框默认选中
      // this.form={
      //   latefee:0,
      //   formula:"单价*数量",
      // }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChargeitem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收费项管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateChargeitem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addChargeitem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除收费项管理编号为"' + ids + '"的数据项？').then(function() {
        return delChargeitem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('feeSettings/chargeitem/export', {
        ...this.queryParams
      }, `chargeitem_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
